package com.jxt.myjxtmanager.ui.model;

import android.content.Context;
import android.text.TextUtils;

import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.okHttp.Result;
import com.jxt.myjxtmanager.okHttp.ResultCallback;
import com.jxt.myjxtmanager.utils.Config;
import com.jxt.myjxtmanager.utils.SharePreferenceUtils.SharedPrefUtil;
import com.jxt.myjxtmanager.views.CustomProgressDialog;


/**
 * Created by Administrator on 2016/1/20 0020.
 */
public class LoginMo {
    private String username, password;
    private OnloginResultListener onloginResultListener;
    private Context context;

    public LoginMo(Context context) {
        this.context = context;
    }

    /**
     * 表单校验
     */
    public boolean MoValidate() {
        return !(TextUtils.isEmpty(username) || TextUtils.isEmpty(password));
    }

    /**
     * 得到用户名
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 得到密码
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 登陆操作
     */
    public void httpLogin(OnloginResultListener onloginResultListener) {
        this.onloginResultListener = onloginResultListener;
//        HttpClient.getInstance().login(context, username, password, new callbackSalesLogin());
        onloginResultListener.onSuccess(null);
    }

    /**
     * 接口回调
     */
    class callbackSalesLogin extends ResultCallback<Result<SaleUser>> {
        //           response.body： {"code":1,"error_code":0,"result":{"driverIncomeList":[],"driverIncomeSum":0.0},"msg":null,"success":true,"failed":false}

        CustomProgressDialog dialog;

        @Override
        public void onStart() {
            dialog = new CustomProgressDialog(context, "");
            dialog.showDialog();
        }

        @Override
        public void onResponse(Result<SaleUser> jsonObjectResult) {
            SaleUser mResultSaleUser = (SaleUser) jsonObjectResult.getResult();
            if (saveSaleUser(mResultSaleUser)) {//如果返回用户信息不为空
                onloginResultListener.onSuccess(mResultSaleUser);
            }
        }

        @Override
        public void onResponse(int code, String msg) {
            onloginResultListener.onFailure(code, msg);
        }

        @Override
        public void onError(int code, String message) {
            onloginResultListener.onFailure(2, message);
        }

        @Override
        public void onFinish() {
            if (dialog != null) {
                dialog.dismiss();
                dialog = null;
            }
        }
    }

    /**
     * 保存用户信息
     */
    public boolean saveSaleUser(SaleUser saleUser) {
        SharedPrefUtil sp = SharedPrefUtil.getInstance(context);
        String token = "";
        if (saleUser != null) {
            token = saleUser.getToken();
        }
        if (!TextUtils.isEmpty(token)) {
            sp.putString(Config.SHARED_TOKEN, saleUser.getToken());
            sp.commit();
            SaleUserModel.getInstance().insertOrReplace(saleUser); // 保存登录返回的用户信息
            return true;
        } else {
            onloginResultListener.onFailure(2, "token 错误");
            return false;
        }
    }

    /**
     * 数据请求监听器
     */
    public interface OnloginResultListener {
        void onSuccess(SaleUser saleUser);

        void onFailure(int code, String msg);//1普通 2.token错误
    }
}
